package com.zy.dingdong.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.zy.dingdong.R;
import com.zy.dingdong.activity.BaseActivity;
import com.zy.dingdong.activity.ChatListActivity;
import com.zy.dingdong.activity.FriendListActivity;
import com.zy.dingdong.activity.MineActivity;

public class Navigation extends LinearLayout implements View.OnClickListener {

    private static final String TAG = "HELLO";
    private LinearLayout lChat,lContact,lMine;

    private ImageView openChatList, openFriendList, openMine, openCurrent;

    private Context context;
    private Activity activity;

    public Navigation(final Context context, AttributeSet attrs)  {
        super(context, attrs);
        this.context = context;
        this.activity = (Activity)context;
        LayoutInflater.from(context).inflate(R.layout.navigation,this);

        lChat = findViewById(R.id.tab_xiaoxi);
        lChat.setOnClickListener(this);
        lContact = findViewById(R.id.tab_tongxunlu);
        lContact.setOnClickListener(this);
        lMine = findViewById(R.id.tab_wode);
        lMine.setOnClickListener(this);

        //ImageView
        openChatList = findViewById(R.id.open_chat_list);
        openFriendList = findViewById(R.id.open_friend_list);
        openMine = findViewById(R.id.open_mine);

        int bottomFlag = BaseActivity.pref.getInt("bottom_flag",1);
        //默认消息按钮被选中
        if (bottomFlag == 1){
            openChatList.setSelected(true);
            openCurrent = openChatList;
        }else if (bottomFlag == 2) {
            openFriendList.setSelected(true);
            openCurrent = openFriendList;
        }else {
            openMine.setSelected(true);
            openCurrent = openMine;
        }

    }

    @Override
    public void onClick(View view) {
        changeTable(view);
    }
    private void changeTable(View view){
        openCurrent.setSelected(false);
        switch (view.getId()){
            case R.id.tab_xiaoxi:
                BaseActivity.prefEditor.putInt("bottom_flag",1);
                BaseActivity.prefEditor.commit();
                Intent intent1 = new Intent(context, ChatListActivity.class);
                context.startActivity(intent1);
                break;
            case R.id.tab_tongxunlu:
                BaseActivity.prefEditor.putInt("bottom_flag",2);
                BaseActivity.prefEditor.commit();
                Intent intent2 = new Intent(context, FriendListActivity.class);
                context.startActivity(intent2);
                break;
            case R.id.tab_wode:
                BaseActivity.prefEditor.putInt("bottom_flag",3);
                BaseActivity.prefEditor.commit();
                Intent intent3 = new Intent(context, MineActivity.class);
                context.startActivity(intent3);
                break;
            default:
                break;
        }
    }
}
